package com.jintoufs.web.action.customer;

import com.alibaba.fastjson.JSON;
import com.jhj.common.util.JhjResponseUtil;
import com.jintoufs.domain.customer.CustomerResource;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.customer.CustomerResourceService;
import com.jintoufs.util.ztree.TreeNode;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jintoufs.util.ResponseUtil.writeInfo;

/**
 * 资源信息前端控制器
 */
@Controller
@Scope("prototype")
@RequestMapping("security/supplier/resource")
public class CustomerResourceAction {
    @Autowired
    private CustomerResourceService customerResourceService;

    @RequestMapping(value = "/list")
    @SystemControllerLog(description = "获取资源列表")
    public String list(Model model) {
        List<CustomerResource> resourceList = customerResourceService.findByParams(null);

        List<Object> resourceLists = customerResourceService.getResourceTreeList(resourceList);
        resourceLists.remove(0);

        model.addAttribute("listEntity", resourceLists);
        return "supplier/resource/list";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @SystemControllerLog(description = "新增资源")
    public void save(CustomerResource resource, HttpServletResponse response) {
        int parentId;
        Map<String, Object> params = new HashMap<String, Object>();
        if (resource.getParentId() == null) { //父节点
            parentId = 0;
            resource.setParentId(parentId);
            resource.setParentIds("0/");
        } else { //子节点
            parentId = resource.getParentId();
            params.put("id", parentId);
            List<CustomerResource> list = customerResourceService.findByParams(params);
            if (list != null && list.size() > 0) {
                CustomerResource parent = list.get(0);
                resource.setParentIds(parent.makeSelfAsParentIds());
            }
        }
        Map<String, Object> result = customerResourceService.save(resource);
        JhjResponseUtil.responseJson(response, JSON.toJSONString(result));
    }

    @RequestMapping("edit")
    @SystemControllerLog(description = "修改资源")
    public void edit(CustomerResource resource, HttpServletResponse response) {
        Map<String, Object> result = customerResourceService.save(resource);
        JhjResponseUtil.responseJson(response, JSON.toJSONString(result));
    }

    /**
     * input.do?id= 进入类目编辑页面 <br>
     * input.do 进入类目新增页面（不包含选中父类目）
     *
     * @param id    类目Id
     * @param model
     * @return vm/category/input.vm
     */
    @RequestMapping(value = "input", method = RequestMethod.GET)
    public String input(Integer id, Model model) {
        CustomerResource entity = null;

        // 是否为编辑保存
        Boolean isEdit = false;

        if (id != null) {
            // 修改
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id", id);
            List<CustomerResource> list = customerResourceService.findByParams(params);
            if (list != null && list.size() > 0) {
                entity = list.get(0);
            }
            isEdit = true;
        } else {
            // 新增
        }

        List<TreeNode> list = customerResourceService.getModuleTree(entity);
        model.addAttribute("listTree", JSON.toJSONString(list));
        model.addAttribute("isEdit", isEdit);
        model.addAttribute("entity", entity);
        return "supplier/resource/input";
    }

    @RequestMapping("delete")
    @SystemControllerLog(description = "删除资源")
    public void delete(HttpServletResponse response, Integer id) {
        if (id != null && id != 0) {
            customerResourceService.delete(id);
            //同时删除该资源与用户的关系
            customerResourceService.deleteResourceById(id);
        }
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }
}
